import {
  ArgumentMetadata,
  BadRequestException,
  PipeTransform,
} from '@nestjs/common';

/**
 * 设置数字默认参数（比如页码）
 */
export class ParseOptionalIntPipe implements PipeTransform<string, number> {
  constructor(private readonly defaultValue?: number) {}

  transform(value: string, metadata: ArgumentMetadata): number {
    if (!value) {
      return this.defaultValue;
    }
    const parsedValue = parseInt(value, 10);
    if (isNaN(parsedValue)) {
      throw new BadRequestException(
        `Validation Failed.${value} is not an integer`,
      );
    }
    return parsedValue;
  }
}
